<template>
  <!-- 订单组件 -->
  <div class="body">
    <div class="neijuanj">
      <van-pull-refresh
        v-model="isLoading"
        success-text="刷新成功"
        @refresh="onRefresh"
      >
        <div class="bianju">
          <ul class="shangp" v-if="datas.length > 0">
            <li
              class="conten"
              v-for="(item, index) in datas"
              :key="index"
              @click.stop="todingagn(item.id)"
            >
              <div class="neirbuf">
                <div class="img">
                  <img :src="item.goodsList[0].picUrl" alt="" />
                </div>
                <div class="texts">
                  <ul>
                    <li class="title">{{ item.goodsList[0].goodsName }}</li>
                    <li class="guige">
                      <span>规格：</span
                      ><span
                        v-for="(i, d) in item.goodsList[0].specifications"
                        :key="d"
                        >{{ i }}</span
                      >
                    </li>
                    <li class="zhuantai">
                      <span>{{ item.orderStatusText }}</span>
                    </li>
                  </ul>
                </div>
                <div class="jiangge">
                  <ul>
                    <li class="danjia">
                      <span>{{ item.goodsList[0].price }}</span
                      >￥
                    </li>
                    <li class="shuliang">
                      <span>{{ item.goodsList[0].number }}</span
                      >x
                    </li>
                    <li class="zhojian">
                      <span
                        >需付款：<span>￥</span
                        ><span>{{ item.actualPrice }}</span></span
                      >
                    </li>
                  </ul>
                </div>
              </div>
              <div class="mans">
                <div class="gonneng">
                  <div class="fukuan">
                    <span
                      v-if="item.handleOption.pay"
                      @click.stop="fukuan(item.id)"
                      >付款</span
                    >
                    <span
                      v-if="item.handleOption.cancel"
                      class="qiuxiao"
                      @click.stop="quxiao(item.id)"
                      >取消订单</span
                    >
                    <span
                      v-if="item.handleOption.aftersale"
                      @click.stop="pingjia(item)"
                      >评论</span
                    >

                    <span v-if="item.handleOption.comment">已评论</span>

                    <span
                      v-if="item.handleOption.delete"
                      @click.stop="ondelete(item.id)"
                      >删除订单</span
                    >
                    <span
                      v-if="item.handleOption.confirm"
                      @click.stop="take(item.id)"
                      >确定收货</span
                    >
                    <!-- <span v-if="item.handleOption.confirm">待发货</span> -->
                    <span v-if="item.handleOption.aftersale"> 售后 </span>
                    <span
                      v-if="item.handleOption.refund"
                      @click.stop="tkuan(item.id)"
                    >
                      退款</span
                    >
                  </div>
                </div>
              </div>
            </li>
          </ul>
          <ul v-else class="zhanwu">
            <li>
              <img src="../../assets/sp.png" alt="" />
            </li>
            <li>
              <span>暂无商品</span>
            </li>
          </ul>
        </div>
      </van-pull-refresh>
    </div>

    <!-- <div style="height: 10px"></div> -->
  </div>
</template>

<script>
import { Toast } from "vant";
import { PullRefresh } from "vant";
import API from "../../http/API";
export default {
  name: "dingdan",
  // 传入页数
  props: ["yeshu"],
  inject: ["reload"],
  components: {
    [PullRefresh.name]: PullRefresh,
    [Toast.name]: Toast,
  },
  watch: {
    // $route: {
    //   // handler(to) {
    //   //   console.log(to);
    //   // },
    // },
    yeshu(e) {
      console.log(e);
      if (this.indexpages <= this.pages) {
        this.indexpages = e;
        this.http();
      }
    },
  },
  data() {
    return {
      datas: [],
      isLoading: false,
      TiShiText: "",
      pages: "",
      indexpages: 1,
    };
  },
  activated() {
    // 通过页面下标来拿
    this.http();
  },
  // watch: {
  //   // shujut(val) {
  //   //   // console.log(val);
  //   //   this.datas = val;
  //   //   // console.log(this.datas);
  //   // },
  // },

  created() {
    // console.log(this.$route.meta.active);
    this.http();
  },
  activated() {
    // this.guanb();
  },
  methods: {
    // 付款

    fukuan(e) {
      this.tishi();
      this.TiShiText = "付款";
      // console.log(e);
      this.$post(API.POST_ORDER_PAY, {
        token: this.$store.state.token,
        orderId: this.$route.meta.active,
      })
        .then((res) => {
          // console.log(res);

          this.http(1);
        })
        .catch((err) => {});
    },
    // 确定收货
    take(e) {
      console.log(e);
      this.TiShiText = "收货";
      this.tishi();
      this.$post(API.POST_ORDER_CONFIRM, {
        token: this.$store.state.token,
        orderId: e,
      })
        .then((res) => {
          console.log(res);

          this.http(1);
        })
        .catch((err) => {});
    },
    // 取消订单
    quxiao(e) {
      this.tishi();
      this.TiShiText = "取消";
      // console.log(e);
      this.$post(API.POST_ORDER_CANCEL, {
        token: this.$store.state.token,
        orderId: e,
      })
        .then((res) => {
          // console.log(res);

          this.http(1);
        })
        .catch((err) => {});
    },
    // 删除订单
    ondelete(e) {
      this.tishi();
      this.TiShiText = "删除";
      this.$post(API.POST_ORDER_DELETE, {
        token: this.$store.state.token,
        orderId: e,
      })
        .then((res) => {
          // console.log(res);

          this.http(1);
        })
        .catch((err) => {});
    },
    // 退款
    tkuan(e) {
      this.TiShiText = "退款";
      this.$post(API.POST_ORDER_REFUND, {
        token: this.$store.state.token,
        orderId: e,
      })
        .then((res) => {
          // console.log(res);

          this.http(1);
        })
        .catch((err) => {});
    },
    http(e) {
      this.$get(API.GET_ORDER_LIST, {
        token: localStorage.getItem("token"),
        showType: this.$route.meta.active,
        page: this.indexpages,
      }).then((res) => {
        // console.log(res);
        // this.reload();
        this.$nextTick(() => {
          // this.datas = [];
          this.datas = this.datas.concat(res.data.list);
          this.pages = res.data.pages;
          console.log(res);
          switch (e) {
            case 0:
              this.isLoading = false;
              break;
            case 1:
              Toast.success(this.TiShiText + "成功");
              Toast.clear();

              break;
          }
        });
      });
    },

    // 提示调用
    tishi() {
      const toast = Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: this.TiShiText + "中...",
      });
    },
    onRefresh() {
      this.http(0);
    },

    todingagn(e) {
      // console.log(e);

      this.$router.push({
        path: "/orderdetails",
        query: {
          id: e,
        },
      });
    },
    // 前往评价
    pingjia(e) {
      console.log(e);
      this.$router.push({
        path: "/pingjia",
        query: {
          shangpin: JSON.stringify(e),
        },
      });
    },
  },
  deactivated() {
    window.removeEventListener("scroll", this.handleScroll);
  },
};
</script>

<style lang="less" scoped>
.body {
  height: 100%;
  // margin-top: 10px;
  // // margin-top: 80px;
  .neijuanj {
    // margin-top: 10px;
  }
  .zhanwu {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100%;
    li {
      margin-top: 20px;
      width: 200px;
      height: 200px;
      text-align: center;
      span {
        font-size: 18px;
        color: rgb(71, 70, 70);
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  background-color: @background;
}
.bianju {
  margin-top: 75px;
  // padding-bottom: 50px;
}
.shangp {
  // width: 100%;
  // height: 100px;
  margin-left: 5px;
  margin-right: 5px;
  // margin-top: 10px;
  padding-bottom: 10px;
  // border: 1px solid #000;
  display: flex;
  align-items: center;
  flex-direction: column;

  .conten {
    // height: 100px;
    margin-top: 10px;
    width: 100%;
    display: flex;
    // align-items: center;
    flex-direction: column;
    border-radius: 5px;
    background-color: rgb(255, 255, 255);
    .neirbuf {
      margin-top: 5px;
      margin-right: 5px;
      margin-left: 5px;
      display: flex;
      // width: 100%;
      .texts {
        margin-left: 5px;
        flex: 1;
        width: 100%;
      }
      .img {
        width: 70px;
        border-radius: 5px;
        overflow: hidden;
        height: 70px;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
.title {
  margin-top: 5px;
  font-size: 17px;
  color: rgb(39, 39, 39);
  margin-bottom: 5px;
}
.guige {
  margin-bottom: 5px;
  color: rgb(138, 138, 138);
}
.zhuantai {
  color: rgb(247, 112, 112);
}
.mans {
  display: flex;
  flex-direction: row-reverse;
  .gonneng {
    margin-top: 5px;
    margin-left: 5px;
    margin-right: 5px;
    display: flex;
    flex-direction: column;
    .fukuan {
      //  margin-right: 20px;
      height: 20px;
      // width: 50px;
      border-radius: 5px;
      text-align: center;
      line-height: 20px;
      padding-bottom: 5px;
      span {
        margin-left: 5px;
        color: rgb(233, 103, 103);
        padding: 5px 5px 5px 5px;
        border: 1px solid rgb(219, 117, 117);
        border-radius: 5px;
        // &:nth-child(1){
        //    margin-right: 5px;
        // }
      }
      .qiuxiao {
        margin-left: 5px;
      }
      // border: 1px solid rgb(219, 117, 117);
      margin-bottom: 5px;
    }
  }
}

.jiangge {
  li {
    display: flex;
    flex-direction: row-reverse;
  }
  .zhojian {
    margin-top: 5px;
  }
  .shuliang {
    display: flex;
    margin-top: 5px;
    color: rgb(167, 167, 167);
    flex-direction: row-reverse;
  }
  .danjia {
    margin-top: 5px;
    span {
      font-size: 16px;
    }
  }
}
</style>
